﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace MySqlStudent.UI.Models
{
    /// <summary>
    /// 枚举的扩展类
    /// </summary>
    public static class EnumExtension
    {
        // 获取枚举的名字
        public static string GetDisplayName(this Enum en)
        {
            Type type = en.GetType();
            MemberInfo[] memerInfo = type.GetMember(en.ToString());
            if (memerInfo != null && memerInfo.Length > 0)
            {
                object[] attrs = memerInfo[0].GetCustomAttributes(typeof(DisplayAttribute), true);
                if (attrs != null && attrs.Length > 0)
                {
                    return ((DisplayAttribute)attrs[0]).Name;
                }
            }

            return en.ToString();
        }
    }
}